﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PingSwitch
{
    public partial class frmSettings : Form
    {
        public frmSettings()
        {
            InitializeComponent();
            cbAlertUp.Checked = Properties.Settings.Default.AlertWhenUp;
            cbAlertDown.Checked = Properties.Settings.Default.AlertWhenDown;
            tbPingInterval.Text = Properties.Settings.Default.PingInterval.ToString();
            tbTtl.Text = Properties.Settings.Default.TimeToLive.ToString();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (validInput())
            {
                Properties.Settings.Default.AlertWhenUp = cbAlertUp.Checked;
                Properties.Settings.Default.AlertWhenDown = cbAlertDown.Checked;
                Properties.Settings.Default.Save();
                this.Close();
            }
            else
            {
                MessageBox.Show("Je hebt geen geldige waarden ingevuld.");
            }
        }

        private bool validInput()
        {
            bool result = false;
            try
            {
                int PingInterval;
                PingInterval = Convert.ToInt32(tbPingInterval.Text);
                if (PingInterval > 0 && PingInterval < 100000)
                {
                    Properties.Settings.Default.PingInterval = PingInterval;
                    result = true;
                }
            }
            catch (FormatException e)
            {
                MessageBox.Show("Input string is not a sequence of digits.");
            }
            catch (OverflowException e)
            {
                MessageBox.Show("The number cannot fit in an Int32.");
            }

            try
            {
                int TimeToLive ;
                TimeToLive = Convert.ToInt32(tbTtl.Text);
                if (TimeToLive > 0 && TimeToLive < 256)
                {
                    Properties.Settings.Default.TimeToLive = TimeToLive;
                    result = true;
                }
            }
            catch (FormatException e)
            {
                MessageBox.Show("Input is geen getal.");
            }
            catch (OverflowException e)
            {
                MessageBox.Show("Het getal is geen geldig Int32 getal.");
            }

            return result;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
